<?php

/**
 * Form Checkbox Element class
 *
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
class Mac_Form_Element_Checkbox extends Mac_Form_Element_Abstract
{
    protected $realValue;

    protected $multiple = false;

    public function __construct($id, $name, $value, $label = null, $multiple = false)
    {
        parent::__construct($name);
        $this->id = $id;
        $this->realValue = (string)$value;
        $this->label = $label;
        $this->multiple = (boolean)$multiple;
    }

    public function setMultiple ($is = true)
    {
        $this->multiple = (boolean)$is;
        return $this;
    }

    public function __toString ()
    {
        return '<input
            type="checkbox"
            id="'.$this->id.'"
            name="'.$this->name.(($this->multiple === true) ? '[]' : '').'"
            value="'.$this->realValue.'"
            '.(($this->isChecked() === true) ? 'checked="checked"' : '').'
            '.((count($this->class) > 0) ? 'class="'.implode(' ', $this->class).'"' : '').'
            '.$this->printOptions().'
        />';
    }

    protected function isChecked ()
    {
        if(is_array($this->value)) {
            return (in_array($this->realValue, $this->value));
        } else {
            return ($this->realValue === $this->value);
        }
    }
    
}